<?php
require_once("func.php");

main($argv[1]);

function main($input) {
	$content = file_get_contents($input);
	$info = get_info($content);
	insert_resume($info);
	insert_work($info);
	insert_project($info);
	insert_edu($info);
	//insert_resume_index($info);
	//insert_work_index($info);
	//insert_project_index($info);
	//insert_edu_index($info);
}

function get_info($html) {
	$html = strip_rn($html);
	$basic_info = get_basic_info($html);
	$work_info = get_work_info($html);
	$project_info = get_project_info($html);
	$edu_info = get_edu_info($html);
	$info = array(
		'basic' => $basic_info,
		'work' => $work_info,
		'project' => $project_info,
		'edu' => $edu_info,
	);
	var_dump($info);
	return $info;
}

function get_project_info($html) {
	$pre_p = '|<h3 class="fc6699cc">项目经历<\/h3>(.+?)<h3 class="fc6699cc">教育经历|';
	$res = get_match_groups($html, $pre_p);
	if (isset($res[0])) {
		$html = $res[0];
	} else {
		return array();
	}
	// 开始月份StartMonth，结束月份EndMonth，描述Description，职责Responsibility，软件Software，硬件Hardweare，工具Tool，简历ID
	$patterns = array(
		'StartMonth' => '|<h2>(.+?) - .+?&nbsp;&nbsp;|',
		'EndMonth' => '|<h2>.+? - (.+?)&nbsp;&nbsp;|',
		'Description' => '|<td width="60">项目描述：<\/td>(.+?)<\/tr>|',
		'Responsibility' => '|<td width="60">责任描述：<\/td>(.+?)<\/tr>|',
		'Title' => '|<h2>(.+?)<\/h2>|',
		'Software' => '||',
		'Hardweare' => '||',
		'Tool' => '||',
	);
	$arr_info = array();
	foreach ($patterns as $cate => $p) {
		if (empty($p)) {
			continue;
		}
		$res = get_match_groups($html, $p);
		if (isset($res[0])) {
			$arr_info[$cate] = $res;
		}
	}
	$arr_info = info_regroup($arr_info);
	foreach ($arr_info as $ind => $row) {
		if (isset($arr_info[$ind]['Description'])) {
			$arr_info[$ind]['Description'] = $row['Title'] . "。 " . trim(strip_tags($arr_info[$ind]['Description']));
		}
		if (isset($arr_info[$ind]['Responsibility'])) {
			$arr_info[$ind]['Responsibility'] = trim(strip_tags($arr_info[$ind]['Responsibility']));
		}
	}

	return $arr_info;
}

function get_edu_info($html) {
	//$pre_p = '|<h3 class="fc6699cc">教育经历<\/h3>(.+?)<div class="resume-preview-all">|';
	$pre_p = '|<h3 class="fc6699cc">教育经历<\/h3>(.+?)<\/div>|';
	$res = get_match_groups($html, $pre_p);
	if (isset($res[0])) {
		$html = $res[0];
	} else {
		return array();
	}
	// 开始月份，结束月份，学校id，专业，学位id，简历id
	$patterns = array(
		'StartMonth' => '|(\d{4}\.\d{2}) - \d{4}\.\d{2}&nbsp;&nbsp;|',
		'EndMonth' => '|\d{4}\.\d{2} - (\d{4}\.\d{2})&nbsp;&nbsp;|',
		'University' => '|\d{4}\.\d{2} - \d{4}\.\d{2}&nbsp;&nbsp;([^;]+?)&nbsp;&nbsp;[^;]+?&nbsp;&nbsp;[^;]+?|',
		'Major' => '|\d{4}\.\d{2} - \d{4}\.\d{2}&nbsp;&nbsp;[^;]+?&nbsp;&nbsp;([^;]+?)&nbsp;&nbsp;[^;]+?|',
		'Degree' => '|\d{4}\.\d{2} - \d{4}\.\d{2}&nbsp;&nbsp;[^;]+?&nbsp;&nbsp;[^;]+?&nbsp;&nbsp;(.+?)<br|',
	);
	$arr_info = array();
	foreach ($patterns as $cate => $p) {
		if (empty($p)) {
			continue;
		}
		$res = get_match_groups($html, $p);
		if (isset($res[0])) {
			$arr_info[$cate] = $res;
		}
	}
	//array_trim($arr_info['Industry']);
	$arr_info = info_regroup($arr_info);
	//var_dump($arr_info);

	return $arr_info;
}



function get_work_info($html) {
	$pre_p = '|<h3 class="fc6699cc">工作经历<\/h3>(.+?)<div class="resume-preview-all">|';
	$res = get_match_groups($html, $pre_p);
	if (isset($res[0])) {
		$html = $res[0];
	} else {
		return array();
	}
	// 工作经历
	// 开始月份StartMonth，结束月份EndMonth，公司id Company，行业id Industry，部门名称Department，职位id Position，汇报对象ReportTo，下属人数Underling，离职原因Reason，成就Achievement，ResumenId
	$patterns = array(
		'StartMonth' => '|<h2>(.+?) - .+?&nbsp;&nbsp;|',
		'EndMonth' => '|<h2>.+? - (.+?)&nbsp;&nbsp;|',
		'Company' => '|<h2>[^;]+?&nbsp;&nbsp;([^;]+?)&nbsp;&nbsp;[^;]+?<\/h2>|',
		'Industry' => '|<\/h5>.+?<div class="resume-preview-dl">(.+?)\||',
		'Department' => '|<h5>(.+?)<\/h5>|',
		'Position' => '',
		'ReportTo' => '',
		'Underling' => '',
		'Reason' => '',
		'Achievement' => '',
		//'ResumenId' => '',
	);
	$arr_info = array();
	foreach ($patterns as $cate => $p) {
		if (empty($p)) {
			continue;
		}
		$res = get_match_groups($html, $p);
		if (isset($res[0])) {
			$arr_info[$cate] = $res;
		}
	}
	//$arr_info['Industry'] = trim($arr_info['Industry']);
	array_trim($arr_info['Industry']);
	$arr_info = info_regroup($arr_info);

	return $arr_info;
}

function info_regroup($arr_raw) {
	$final = array();
	foreach ($arr_raw as $key => $list) {
		foreach ($list as $lkey => $value) {
			$final[$lkey][$key] = $value;
		}
	}
	return $final;
}

function get_basic_info($html) {
	// 基础信息
	// 姓名Name，性别Gender，出生年月BirthYear，现工作WorkAtNow，期望工作WorkAtExpect，是否出国Abroad，现工资SalaryNow，期望工资SalaryExpect，手机Mobile，emailEmail，职位Position，行业Industry，工作年限WorkMonth，学历Degree，是否在职WorkToNow
	$basic_patterns = array(
		'Name' => '|<div class="main-title-fl fc6699cc">(.+?)<\/div>|',
		'Gender' => '',
		'BirthYear' => '|\d*岁((\d{4})年\d*月)|',
		'WorkAtNow' => '|<h3 class="fc6699cc">工作经历<\/h3>.*?<h2>.+?&nbsp;&nbsp;(.+?)&nbsp;&nbsp;.+?<\/h2>|',
		'WorkAtExpect' => '||',
		'Aborad' => '|(有海外工作\/学习经验)|',
		'SalaryNow' => '',
		'SalaryExpect' => '',
		'Mobile' => '|<div class="main-title-fr">手机 ：(\d+?)<\/div>|',
		'Email' => '|E-mail：<a.+?>(.+?)<\/a>|',
		'Position' => '|<h3 class="fc6699cc">工作经历<\/h3>.+?<h5>(.+?)<\/h5>|',
		'Industry' => '|<div class="resume-preview-dl">(.+?)\||',
		'WorkMonth' => '|(\d+?)年工作经验|',
		'Degree' => '|&nbsp;&nbsp;&nbsp;&nbsp;([^n]+?)&nbsp;&nbsp;&nbsp;&nbsp;[^n]+?<\/span>|',
		'WorkToNow' => '',
	);
	$arr_basic = array();
	foreach ($basic_patterns as $cate => $p) {
		if (empty($p)) {
			continue;
		}
		$basic_res = get_match_groups($html, $p);
		if (isset($basic_res[0])) {
			$arr_basic[$cate] = $basic_res[0];
		}
	}
	$arr_basic['Email'] = strip_tags($arr_basic['Email']);
	$arr_basic['Industry'] = trim($arr_basic['Industry']);
	$arr_basic['WorkMonth'] = 12 * $arr_basic['WorkMonth'];

	return $arr_basic;
}
